﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<script src="string-extensions.js"></script>
	<!-- 5. Write a function that replaces non breaking white-spaces in a text with &nbsp; -->
	<style>
		textarea, button {
			display: block;
			width: 800px;
		}
	</style>
</head>
<body>
	<textarea id="input" placeholder="Text to change"></textarea>
	<button onclick="doChange()">Change Text</button>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// '\xA0'						// Non-breakable space is char 0xa0 (160 dec)
		// String.fromCharCode(160);	// Non-breakable space is char 160
		function replaceWhiteSpaces(input) {
			return input.replace(/\s/g, '&nbsp;');
		}

		// Button OnClick
		function doChange() {
			// Read User input
			var input = jsConsole.read("#input");

			// Output
			jsConsole.writeLine("You have entered (escaped):");
			jsConsole.writeLine("<blockquote>" + input.htmlEscape() + "</blockquote>");

			// Replace all spaces with &nbsp;
			var result = replaceWhiteSpaces(input);
			jsConsole.writeLine("Non-Escaped Result:");
			jsConsole.writeLine("<blockquote>" + result + "</blockquote>");
			jsConsole.writeLine("Escaped Result:");
			jsConsole.writeLine("<blockquote>" + result.htmlEscape() + "</blockquote>");
		}
	</script>
</body>
</html>
